//
//  CDTestViewController6.m
//  CuriosityDaily3
//
//  Created by JSY on 16/9/23.
//  Copyright © 2016年 theBestTeam. All rights reserved.
//

#import "CDTestViewController6.h"
#import "CDSetAccountViewController.h"

#import "CDSettingFirstCell.h"
#import "CDSettingSecondCell.h"
#import "CDSettingThirdCell.h"

#import "CDSettingModel.h"
#import "CDSettingPF.h"

// 第三种
static NSString *twoLabelCellid = @"twoLabelCellid";
// 第一种cell
static NSString *firstCellid = @"firstCellid";
// 第二种cell （三个按钮）
static NSString *secondeCellid = @"secondeCellid";

@interface CDTestViewController6 ()<UITableViewDelegate, UITableViewDataSource, CDSettingSecondCellDelegate>
/** 第一种cell数组*/
@property(nonatomic,strong) NSArray *firstCellTitleDatas;
/** 第二种cell数组*/
@property(nonatomic,strong) NSArray *secondeCellDatas;

@property(nonatomic,strong) UITableView *tableView;
/** 设置字体大小的block*/
@property(nonatomic,copy) void(^fontBlock)(CGFloat);


@property(nonatomic,copy) NSString *accountSetTitle;

@end

@implementation CDTestViewController6

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setNbar];
    [self loadData];
    self.view.backgroundColor = [UIColor darkGrayColor];
    [self setupUI];
    
    [self setNavBarWithTitle:@"设置"];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}

- (void)setNbar {
    self.navigationController.navigationBar.titleTextAttributes = @{ NSForegroundColorAttributeName: [UIColor whiteColor] };
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    
}

/**
 *  设置界面
 */
- (void)setupUI {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(64, 0, 0, 0));
    }];
    
    self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[CDSettingThirdCell class] forCellReuseIdentifier:twoLabelCellid];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CDSettingFirstCell" bundle:nil] forCellReuseIdentifier:firstCellid];
    [self.tableView registerClass:[CDSettingSecondCell class] forCellReuseIdentifier:secondeCellid];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
#warning 判断是否登录，来设置显示的内容
    self.accountSetTitle = @"账号设置";
}


#pragma mark - table view data source 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0 || section == 1) {
        return 1;
    } else {
        return 3;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell;
    
    if (indexPath.section == 0) {
#warning 判断账号是否登录
        // 第一种cell
        cell = [self firstCellWithIndexPath:indexPath leftTitle:self.accountSetTitle];
    } else if (indexPath.section == 1) {
        // 第二种cell
        cell = [self secondCellWithIndexPath:indexPath];
        
    } else if (indexPath.section == 2) {
        if (indexPath.row == 0) {
            cell = [self firstCellWithIndexPath:indexPath leftTitle:@"推送消息"];
            [(CDSettingFirstCell *)cell setRightLabelText:@"未开启"];
        } else {
            // 第三种cell
            cell = [self otherCellWithIndexPath:indexPath leftTitle:self.secondeCellDatas[indexPath.row - 1]];
        }
    } else {
        cell = [self firstCellWithIndexPath:indexPath leftTitle:self.firstCellTitleDatas[indexPath.row]];
    }
    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UITableViewCell *)secondCellWithIndexPath:(NSIndexPath *)indexPath{
    
    CDSettingSecondCell *cell = [self.tableView dequeueReusableCellWithIdentifier:secondeCellid forIndexPath:indexPath];
    cell.delegate = self;
    return cell;
}

- (UITableViewCell *)firstCellWithIndexPath:(NSIndexPath *)indexPath leftTitle:(NSString *)leftTitle  {
     CDSettingFirstCell *cell = [self.tableView dequeueReusableCellWithIdentifier:firstCellid forIndexPath:indexPath];
    [cell setLeftTitle:leftTitle arrow:YES];
    [cell setRightLabelText:@""];
    return cell;
}
- (UITableViewCell *)otherCellWithIndexPath:(NSIndexPath *)indexPath leftTitle:(NSString *)leftTitle {
    CDSettingThirdCell *cell = [self.tableView dequeueReusableCellWithIdentifier:twoLabelCellid forIndexPath:indexPath];
    [cell setLeftButtonWIthTitle:leftTitle];
    
    if (indexPath.row == 1) {
        BOOL update = [[[NSUserDefaults standardUserDefaults] valueForKey:@"update"] boolValue];
        if (update == NO) {
            [cell setSwitchViewState:CDSwitchStateNormal];
        } else {
            [cell setSwitchViewState:CDSwitchStateSelected];
        }
    } else {
        BOOL comment = [[[NSUserDefaults standardUserDefaults] valueForKey:@"comment"] boolValue];
        if (comment == NO) {
            [cell setSwitchViewState:CDSwitchStateNormal];
        } else {
            [cell setSwitchViewState:CDSwitchStateSelected];
        }
    }
    
    return cell;
}

#pragma mark - table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
#warning 如果没有登录就跳转到登录界面， 登录了就push到账号设置界面
    if (indexPath.section ==0) {
        CDSetAccountViewController *AccountVC = [[CDSetAccountViewController alloc] init];
//        AccountVC.title = self.accountSetTitle;
        [AccountVC setNavBarWithTitle:self.accountSetTitle];
        [self.navigationController pushViewController:AccountVC animated:YES];
    }
    
    
//    NSLog(@"点击了%zd组,%zd行",indexPath.section,indexPath.row);
    if (indexPath.section == 2 && indexPath.row != 0) {
        CDSettingThirdCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        
        CDSwitchState switchState = cell.switchState;
        if (indexPath.row == 1) {
            BOOL update = [[[NSUserDefaults standardUserDefaults] valueForKey:@"update"] boolValue];
            if (update == NO) {
                switchState = CDSwitchStateSelected;
                [CDSettingPF sharedSettingPF].update = [NSNumber numberWithBool:YES];
            } else {
                switchState = CDSwitchStateNormal;
                [CDSettingPF sharedSettingPF].update = [NSNumber numberWithBool:NO];
            }
            
        }
        if (indexPath.row == 2) {
            BOOL comment = [[[NSUserDefaults standardUserDefaults] valueForKey:@"comment"] boolValue];
            if (comment == NO) {
                switchState = CDSwitchStateSelected;
                [CDSettingPF sharedSettingPF].comment = [NSNumber numberWithBool:YES];
            } else {
                switchState = CDSwitchStateNormal;
                [CDSettingPF sharedSettingPF].comment = [NSNumber numberWithBool:NO];
            }
        }
        
        [cell setSwitchViewState:switchState];
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if (section == 0) {
        return 10;
    } else if (section == 1) {
        return CDSettingViewSectionSuperMagin;
    }
    return CDSettingViewSectionMagin;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

    UILabel *lable = [[UILabel alloc] init];
    if (section == 1) {
        lable.textColor = [UIColor whiteColor];
      lable.text = @"「 好奇驱动你的世界 」";
    lable.font = [UIFont systemFontOfSize:[CDSettingPF sharedSettingPF].fontSize.floatValue];
//        NSLog(@"%f",[[NSUserDefaults standardUserDefaults] floatForKey:@"fontSize"]);
        __weak typeof(self) weakSelf = self;
        self.fontBlock = ^void(CGFloat flag) {
            lable.font = [UIFont systemFontOfSize:flag + 14];
            [CDSettingPF sharedSettingPF].fontSize = [NSNumber numberWithFloat:flag + 14];
            // 改变子控件大小， 需要layoutIfNeeded
            [weakSelf.tableView layoutIfNeeded];
        };
    }

    lable.textAlignment = NSTextAlignmentCenter;
    lable.bounds = CGRectMake(0, 0, CDScreen.size.width, CDSettingViewSectionMagin);
    lable.backgroundColor = [UIColor clearColor];
    
    return lable;
}

#pragma mark - second section cell delegate
- (void)cell:(CDSettingSecondCell *)cell textFontButtonClick:(CDSetButtonType)type {
    CGFloat flag;
    switch (type) {
        case CDSetButtonTypeSmall:
            flag = 0;
            break;
        case CDSetButtonTypeNormal:
            flag = 2;
            break;
        case CDSetButtonTypeBig:
            flag = 4;
            break;
    }
    self.fontBlock(flag);
}

#pragma mark - 数据

- (void)loadData {
    self.firstCellTitleDatas = @[@"给我们打分", @"反馈意见", @"清除缓存"];
    self.secondeCellDatas = @[@"接收订阅更新提醒", @"接收评论通知"];
    [self.tableView reloadData];
}

- (void)dealloc {
    self.fontBlock = nil;
}
@end
